package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.BuildConfig;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.Staff;
import com.meichis.yndms.model.TallyPhotos;
import com.meichis.yndms.model.TallyPhotosTypeSub;
import com.meichis.yndms.repository.ApiUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class TallyPhotosManager extends BaseManager {
    private static final String TAG = "TallyPhotosManager";

    private static TallyPhotosManager INSTANCE;

    public static TallyPhotosManager getInstance() {
        if (INSTANCE == null) {
            synchronized (TallyPhotosManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new TallyPhotosManager();
                }
            }
        }
        return INSTANCE;
    }

    public void GetStaffList(final MutableLiveData<Boolean> showDialog, String DMSID, MutableLiveData<List<Staff>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("DMSID", DMSID);

        getRootApi().GetStaffList(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<Staff>>() {
                }.getType(), new IWebServiceCallback<List<Staff>>() {
                    @Override
                    public void onSucceed(int sequence, List<Staff> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);

                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }

    public void getSubTypeList(final MutableLiveData<Boolean> showDialog, String IBMUserID, String IBMDealerID, MutableLiveData<List<TallyPhotosTypeSub>> data) {
        showDialog.setValue(false);

        ArrayMap<String, Object> hs = new ArrayMap<>();
        hs.put("IBMUserID", IBMUserID);
        hs.put("IBMDealerID", IBMDealerID);

        getRootApi().getTallySubType(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<TallyPhotosTypeSub>>() {
                }.getType(), new IWebServiceCallback<List<TallyPhotosTypeSub>>() {
                    @Override
                    public void onSucceed(int sequence, List<TallyPhotosTypeSub> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            List<TallyPhotosTypeSub> subList = new ArrayList<>();
                            TallyPhotosTypeSub subType = new TallyPhotosTypeSub();
                            subType.setID(0);
                            subType.setPhotoTypeName("全部");
                            subList.add(subType);
                            subList.addAll(resultPack);
                            data.setValue(subList);
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        data.setValue(new ArrayList<>());
                        MessageTools.showLongToast(error);
                    }
                }));
    }

    public void getTallyPhotos(final MutableLiveData<Boolean> showDialog, String IBMUserID, String IBMDealerID, String PicState, String PhotoType, String IDate, MutableLiveData<List<TallyPhotos>> data) {
        showDialog.setValue(true);

        ArrayMap<String, Object> hs = new ArrayMap<>();
        hs.put("IBMUserID", IBMUserID);
        hs.put("IBMDealerID", IBMDealerID);
        if (PicState.equals("全部")) {
            hs.put("PicState", "");
        } else {
            hs.put("PicState", PicState);
        }
        if (PicState.equals("理货")) {
            if (PhotoType.equals("全部")) {
                hs.put("PhotoType", "");
            } else {
                hs.put("PhotoType", PhotoType);
            }
        } else {
            hs.put("PhotoType", "");
        }
        hs.put("IDate", IDate);
        getRootApi().getTallyPhotos(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<TallyPhotos>>() {
                }.getType(), new IWebServiceCallback<List<TallyPhotos>>() {

                    @Override
                    public void onSucceed(int sequence, List<TallyPhotos> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                        showDialog.setValue(false);
                    }


                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }

}
